<template>
  <span>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form ref="jenkinsForm" :model="jenkinsForm" :rules="rulesJenkinsForm">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="jenkinsName">
          <el-input v-model="jenkinsForm.jenkinsName" autocomplete="off" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="服务器地址" :label-width="formLabelWidth" prop="jenkinsUrl">
          <el-input v-model="jenkinsForm.jenkinsUrl" autocomplete="off" placeholder="请输入url地址"></el-input>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="userName">
          <el-input v-model="jenkinsForm.userName" autocomplete="off" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="token" :label-width="formLabelWidth" prop="token">
          <el-input v-model="jenkinsForm.token" autocomplete="off" placeholder="token" type="password" :show-password="true" autosize></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="jenkinsForm.password" autocomplete="off" placeholder="访问密码" type="password" :show-password="true" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small"  @click="dialogFormVisible = false"> 取 消 </el-button>
        <!--        "dialogFormVisible = false"-->
        <el-button  size="small"  type="primary" @click="handleOK"> 确 定 </el-button>
      </div>
    </el-dialog>
  </span>
</template>

<script>
import {addConfig, editConfig} from "@/api/jenkins";

export default {
  name: "JenkinsModal",
  props: {
    title: {
      type: String,
      default: ''
    },
    operate: {
      type: String,
      default: 'edit'
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      jenkinsForm: {
        id: '',
        jenkinsName: '',
        jenkinsUrl: '',
        userName: '',
        token: '',
        password: ''
      },
      rulesJenkinsForm: {
        jenkinsName: [
          { required: true, message: '请填写jenkins名称', trigger: 'blur' },
        ],
        jenkinsUrl: [
          { required: true, message: '请输入jenkins服务器地址', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入jenkins访问用户名', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    showDialog: function (index=0, row={}) {
      this.dialogFormVisible = true
      if (this.operate === 'edit') {
        this.jenkinsForm.id = row.id
        this.jenkinsForm.jenkinsName = row.jenkinsName
        this.jenkinsForm.jenkinsUrl = row.jenkinsUrl
        this.jenkinsForm.userName = row.userName
        this.jenkinsForm.token = row.token
        this.jenkinsForm.password = row.password
      }
    },
    handleOK() {
      if (this.operate === 'add') {
        this.addData()
      } else if (this.operate === 'edit') {
        this.editData()
      }
    },
    addData() {
      this.$refs.jenkinsForm.validate((valid) => {
        if (valid) {
          let params = {
            jenkinsName: this.jenkinsForm.jenkinsName,
            jenkinsUrl: this.jenkinsForm.jenkinsUrl,
            userName: this.jenkinsForm.userName,
            token: this.jenkinsForm.token,
            password: this.jenkinsForm.password
          }
          addConfig(params).then(res => {
            console.log(res)
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              });
              this.$emit('fetchData')
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              });
            }
          })
          this.dialogFormVisible = false
        } else {
          this.dialogFormVisible = true
        }
      });
    },
    editData() {
      this.$refs.jenkinsForm.validate((valid) => {
        if (valid) {
          let params = {
            id: this.jenkinsForm.id,
            jenkinsName: this.jenkinsForm.jenkinsName,
            jenkinsUrl: this.jenkinsForm.jenkinsUrl,
            userName: this.jenkinsForm.userName,
            token: this.jenkinsForm.token,
            password: this.jenkinsForm.password
          }
          editConfig(params).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              });
              this.$emit('fetchData')
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              });
            }
          })
          this.dialogFormVisible = false
        } else {
          this.dialogFormVisible = true
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
